<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>


    <!--列表-->
    <el-table :data="employees"  highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="100">
      </el-table-column>
      <el-table-column prop="bedNumber" label="床位编号" width="150">
      </el-table-column>
      <el-table-column prop="bedRemarks" label="备注" width="150">
      </el-table-column>
      <el-table-column prop="bedState" label="床位状态" width="120" sortable>
        <template scope="scope">
          <span style="color: red" v-if="scope.row.bedState === 1">使用中</span>
          <span style="color: green" v-else-if="scope.row.bedState === 0">未使用</span>
          <span style="color: yellow" v-else>未知</span>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" width="220">
      </el-table-column>


      <el-table-column label="操作" width="180">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    抽屉-->
<!--    <el-drawer-->
<!--        title="员工列表"-->
<!--        :visible.sync="table"-->
<!--        direction="rtl"-->
<!--        size="50%">-->
<!--      <el-table :data="gridData">-->
<!--        <el-table-column property="username" label="姓名" width="150"></el-table-column>-->
<!--        <el-table-column property="phone" label="电话" width="200"></el-table-column>-->
<!--        <el-table-column property="age" label="年龄"></el-table-column>-->
<!--        <el-table-column property="email" label="邮箱"></el-table-column>-->
<!--      </el-table>-->
<!--    </el-drawer>-->
		<!--工具条(分页,批量删除)-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑添加界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="床位备注" prop="bedRemarks">
					<el-input v-model="editForm.bedRemarks" auto-complete="off"></el-input>
				</el-form-item>
<!--        <el-form-item label="护工编号" prop="nurseNumber">-->
<!--					<el-input v-model="editForm.nurseNumber" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
				<el-form-item label="床位状态">
					<el-radio-group v-model="editForm.bedState">
						<el-radio class="radio" :label="1">使用中</el-radio>
						<el-radio class="radio" :label="0">未使用</el-radio>
					</el-radio-group>
				</el-form-item>
<!--        <el-form-item label="创建时间" prop="createTime">-->
<!--          <el-input v-model="editForm.createTime" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
			  //抽屉数据
        // gridData:[],
        //默认不弹出抽屉模态框
        // table:false,

        currentPage:1,
        pageSize:5,

        //关键字
				filters: {
					keyWord: ''
				},
				employees: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
          nurseName: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑和添加界面数据
				editForm: {
					id: 0,
          bedRemarks: '',
          bedState: 0,
          createTime: "",
				},
        title:'',
        // options:''


			}
		},
		methods: {
			//性别显示转换
			// formatSex: function (row, column) {
			// 	return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			// },
      //分页页数点击
			handleCurrentChange(val) {
        this.currentPage = val;
				this.getEmployees();
			},
      //查询,查出来的数据从第一页开始展示,而不是当前页
      search() {
        this.currentPage = 1;
				this.getEmployees();
			},
			//获取员工列表
			getEmployees() {
				let para = {
					pageSize: this.pageSize,
					currentPage: this.currentPage,
          keyWord: this.filters.keyWord
				};
				this.listLoading = true;
				this.$http.post("/bed/list",para)
        .then(result=>{
          //关闭忙等
          this.listLoading = false;
          this.employees=result.data.rows
          this.total=result.data.totals

        })
        .catch(result=>{
          this.$message({
            message: '网络不通',
            type: 'error'
          });
        })

			},
      //获取部门数据
      // getDepts(){
			//   this.$http.get("/dept")
      //   .then(Result=>{
      //     this.depts=Result.data
      //   })
      // },
      // getParent(){
      //   this.$http.get("/dept/tree")
      //       .then(Result=>{
      //         this.options=Result.data
      //       })
      // },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(row)
					this.$http.delete("/bed/"+row.id)
          .then(result=>{
              result = result.data
            if (result.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }else {
              this.$message({
                message: '删除失败',
                type: 'error'
              });
            }
            this.listLoading = false;
            this.currentPage=1;
            this.getEmployees();
          })

          .catch(result=>{
            this.$message({
              message: '网络不通',
              type: 'error'
            });

          })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.editFormVisible = true;
				//直接传递为地址传递 this.editForm = row,导致编辑表单后点击取消也会进行修改
				this.editForm = Object.assign({}, row);

			},
			//显示新增界面
			handleAdd: function () {
			  this.title="新增";
				this.editFormVisible = true;
				this.editForm = {
				};
			},
			//编辑和新增
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
				  //判断输入框内是否有数据
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
              //将表单中的数据赋值给空对象 , 直接传递为值传递 this.
							let para = Object.assign({}, this.editForm);
              // // 新增的时候 parentId 传的是数组 但是 编辑的时候 它传的是一个 long
              // if (para.parentId && para.parentId instanceof Array){
              //   // 拿到 parentId 数组的最后一个数值
              //   para.parentId = para.parentId[para.parentId.length - 1]
              // }
							this.$http.put("/bed",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({
                        message: '操作成功',
                        type: 'success'
                      });
                    }else {
                      this.$message({
                        message: '操作失败',
                        type: 'error'
                      });
                    }
                    this.editLoading = false;
                    this.currentPage=1;
                    this.getEmployees();
                    this.editFormVisible = false;
                  })

                  .catch(result=>{
                    this.$message({
                      message: '网络不通',
                      type: 'error'
                    });
                    this.editLoading = false;
                    this.editFormVisible = false;
                  })

						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			  //获取所点击的数据的id的集合
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/bed",ids)
              .then(result=>{
                result = result.data
                if (result.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '删除失败',
                    type: 'error'
                  });
                }
                this.listLoading = false;
                this.currentPage=1;
                this.getEmployees();
              })

				}).catch(() => {

				});
			},

      // doubleClick(row){
      //   this.table=true;
			//   let deptid = row.id;
			//   this.$http.get("/emp/findByDeptid/"+deptid)
      //   .then(result=>{
      //     this.gridData = result.data;
      //   })
      // }
		},
		mounted() {
			this.getEmployees();
			//this.getDepts();
			// this.getParent();
		}
	}

</script>

<style scoped>

</style>